var saveStatus = true;
/*实现编辑*/
mui('.mui-content').on('tap','.edit',function(e){
	$(this).parent().find('.dealerName').removeAttr('readonly').removeClass('readOnly');
	$(this).parent().parent().find('.endMoney').removeAttr('readonly').removeClass('readOnly');
	$(this).parent().find('.dealerName').val($(this).parent().find('.dealerName').val()).focus();
	$(this).hide();
	$(this).parent().find('.save').show();
})

/*实现添加分销商*/
function appendContent(index,endMoney){
	var strAppend ='<div id="'+index+'" class="dealer"><div class="dealerTop"><input type="text" placeholder="请输入级别名称" class="dealerName"/>';
	    strAppend +='<span class="btn save" style="display:block;">保存</span><span class="btn save">保存</span></div>';
	    strAppend +='<div class="dealerBottom clearflex">';
		strAppend +='<i class="startLine"></i><i class="endLine"></i>';
		strAppend +='<div class="startMoneyInfoWrap">';
		strAppend +='<p class="startMoneyInfo">最低消费金额</p>';
		strAppend +='<span class="startMoney  readOnly">'+endMoney+'</span></div>';
		strAppend +='<div class="endMoneyInfoWrap"><p class="endMoneyInfo">最高消费金额</p>';
		strAppend +='<input type="number" class="endMoney"/></div></div></div>';
	$('.mui-content').append(strAppend);
	$('#'+index+'').find('.dealerName').focus();
}
mui('#header').on('tap','.categoryIconAdd',function(e){
	var index = $('.mui-content').find('.dealer').length + 1;
	var endMoney = 0;
	var addStatus = saveJudge();
	if(addStatus){
		if(index == 1){
			appendContent(index,endMoney);
			
		}else{
			endMoney = $('#'+(index-1).toString()+'').find('.endMoney').val();
			var startMoney = $('#'+(index-1).toString()+'').find('.startMoney').html();
			appendContent(index,endMoney);
		}
	}else{
		tip2('请先保存编辑后的信息！');
	}
	$('.edit').on('click',function(){
		$(this).parent().find('.dealerName').val($(this).parent().find('.dealerName').val()).focus();
	})
	//保存
	$('.save').on('click',function(){
		saveEvent($(this));
	})
})



/*实现保存*/
mui('.mui-content').on('tap','.save',function(e){
	saveEvent($(this));
})


//友情提示弹框
function tip(error){
	$('#errorInfo').html(error);
	$('.mui-active').show();
	$('#errorTip').show();
}
function tip2(error){
	$('#errorInfo2').html(error);
	$('.mui-active').show();
	$('.errorTip').show();
}
//友情提示确认
mui('body').on('tap','.errorSubmit',function(e){
	$('.mui-active').hide();
	$('.errorTip').hide();
})

function save(index,dealerName,startMoney,endMoney,userLevelId,obj){
	$.ajax({
		type:"POST",
		url : "/maidian/supplier/saveDistributorLevel.json",
		dataType : "json",
		data : {
			"level" :index,
			"levelName" :dealerName,
			'lowPrice':startMoney,
			'highPrice':endMoney,
			'userLevelId':userLevelId
		},
		success : function(data) {
			if(data["responseEntity"]["code"] == "200"){
				tip('保存成功！');
				$('.queren').on('click',function(){
					$('.mui-active').hide();
					$('#errorTip').hide();
					obj.parent().find('.edit').show();
					obj.hide();
					obj.parent().find('.dealerName').addClass('readOnly').attr('readOnly','readOnly');
					obj.parent().parent().find('.endMoney').addClass('readOnly').attr('readOnly','readOnly');
					saveStatus = true;
					/*window.location.reload();*/
				})
			
			}else if(data["responseEntity"]["code"] == "-200"){
				tip2('保存失败，请重试！');
				saveStatus = false;
			}
			
		}
	})
}

function saveEvent(obj){
	var dealerName = obj.parent().find('.dealerName').val();//分销商描述
	var endMoney = parseInt(obj.parent().parent().find('.endMoney').val());//消费最高金额
	var startMoney = parseInt(obj.parent().parent().find('.startMoney').html());//消费最低金额
	var index = parseInt(obj.parent().parent().attr('id'));
	var next = $('.mui-content').find('#'+(index+1)+'');
	//传递参数
    var userLevelId = obj.attr('userLevelId');
	if(next.length == 0){
		//没有下一级
		if(Boolean(dealerName) && Boolean(endMoney)){
			//信息填写完整
			if(endMoney > startMoney){
				//最高消费金额大于最低消费金额，可提交
				saveStatus = true;
				save(index,dealerName,startMoney,endMoney,userLevelId,obj);
			}else{
				tip2('最高消费金额应大于最低消费金额');
				saveStatus = false;
			}
		}else{
			//信息填写不完整
			if(!Boolean(dealerName)){
				obj.parent().find('.dealerName').focus();
			}else if(!Boolean(endMoney)){
				obj.parent().parent().find('.endMoney').focus();
			}
			/*tip('信息填写不完整');*/
			saveStatus = false;
		}
	}else{
		//有下一级
		if(Boolean(dealerName) && Boolean(endMoney)){
			//信息填写完整
			if(endMoney > startMoney){
				//最高消费金额大于最低消费金额
				if(endMoney <= next.find('.endMoney').val()){
					//submit
					saveStatus = true;
					save(index,dealerName,startMoney,endMoney,userLevelId,obj);
				}else{
					tip2('最高消费金额需小于下一级别最高消费金额');
					saveStatus = false;
				}
			}else{
				tip2('最高消费金额应大于最低消费金额');
				saveStatus = false;
			}
		}else{
			//信息填写不完整
			tip2('信息填写不完整');
			saveStatus = false;
		}
	}
}

/*判断是否保存*/
function saveJudge(){
	var len = $('.mui-content').find('.dealer').length;
	var judgeLen = $('#userLevelNum').val();
	if(((len-judgeLen) <1) && saveStatus){
		return true;
	}else{
		return false;
	}
}